//
//  YYTabBarItem.swift
//  YYWeibo
//
//  Created by strongwallyi on 16/1/6.
//  Copyright © 2016年 strongwallyi. All rights reserved.
//

import UIKit

class YYTabBarItem: UITabBarItem {

    override var badgeValue: String? {
        didSet{
            
            if badgeValue == nil {
                return
            }
            
            //  _target
            // 遍历控件
            guard let tabBarController = valueForKeyPath("_target") as? UITabBarController else {
                return
            }
            let tabBar = tabBarController.tabBar
            
            for tabBarChild in tabBar.subviews {
                // 如果是 UITabBarButton
                if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    // 继续遍历
                    for tabBarButtonChild in tabBarChild.subviews {
                        // 如果是 _UIBadgeView
                        if tabBarButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                            // 继承遍历
                            for badgeViewChild in tabBarButtonChild.subviews {
                                if badgeViewChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                    badgeViewChild.setValue(UIImage(named: "main_badge"), forKeyPath: "_image")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
